package Control;

import Network.NetworkProtocol;
import java.io.IOException;
import java.io.Serializable;

/**
 * <code>InitRequest</code> class instances represent initialization requests.
 * @author Dimon
 */
public class InitRequest extends Command implements Serializable {

    /**
     * Creates new initialization request.
     */
    public InitRequest() {
        Debug("created " + toString());
    }

    /**
     * Sends <code>InitReply</code> with new participant
     * if reciever is a leader.
     */
    @Override
    public void perform() {
        try {
            if (ID.equals(selfID)) {
                Debug("ignoring own InitRequest");
                return;
            }
            if (ID.equals(swim.getLeader())) {
                Debug("'" + ID + "' is not a leader");
                return;
            }
            Debug("responding to InitRequest");
            swim.addParticipant(ID);
            NetworkProtocol.SendCommand(new InitReply());
        } catch (IOException ex) {
            Serious("could not send an InitReply");
        }
    }

    /**
     * Describes request.
     * @return request description.
     */
    @Override
    public String toString() {
        return "initialization request " + super.toString();
    }
}
